/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.view;

import java.util.List;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.example.android.google.apis.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/**
 * Example of how to use an {@link com.google.android.maps.MapView}.
 * <h3>MapViewDemo</h3>

<p>This demonstrates creating a Map based Activity.</p>

<h4>Demo</h4>
Views/MapView

<h4>Source files</h4>
 * <table class="LinkTable">
 *         <tr>
 *             <td >src/com.example.android.apis/view/MapViewDemo.java</td>
 *             <td >The Alert Dialog Samples implementation</td>
 *         </tr>
 *         <tr>
 *             <td >/res/layout/mapview.xml</td>
 *             <td >Defines contents of the screen</td>
 *         </tr>
 * </table>
 */
public class MapViewDemo extends MapActivity {
    private MapView mapView;
    private MapController mc;
    private GeoPoint p;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);
        
    	mapView = (MapView) findViewById(R.id.map_view);
    	mc = mapView.getController();
    	
    	mapView.displayZoomControls(true);
    	mapView.setSatellite(true);

        mc = mapView.getController();
        String coordinates[] = {"38.882232", "-77.113155"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

        p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(17); 

        //---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay(p);
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        

        mapView.invalidate();
    } 
    
    @Override
    protected void onResume() {
    	super.onResume();
    }
    
    @Override
    protected void onPause() {
    	super.onPause();
    }
    
    @Override
    protected boolean isRouteDisplayed() { return false; }

	class MapOverlay extends com.google.android.maps.Overlay {
	    
		private GeoPoint point;
		private Paint paint1;
		
		public MapOverlay(GeoPoint point) {
		    this.point = point;

		    paint1 = new Paint();
		    paint1.setARGB(128, 0, 0, 255);
		    paint1.setStrokeWidth(2);
		    paint1.setStrokeCap(Paint.Cap.ROUND);
		    paint1.setAntiAlias(true);
		    paint1.setDither(false);
		    paint1.setStyle(Paint.Style.STROKE); 
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {

		    Point pt = mapView.getProjection().toPixels(point, null);
		    float radius = (float) Math.pow(2, mapView.getZoomLevel() - 15);

		    if(radius < canvas.getHeight()/25){
		        radius = canvas.getHeight()/25;
		    }

		    canvas.drawCircle(pt.x, pt.y, radius, paint1);

		}
		
		public boolean onTouchEvent(MotionEvent event, MapView mapview){
	
	        if (event.getAction() == 1){
	            GeoPoint p = mapview.getProjection().fromPixels((int)event.getX(), (int)event.getY());
	
	            MapOverlay mapOverlay = new MapOverlay(p);
	            List<Overlay> listOfOverlays = mapView.getOverlays();
	            listOfOverlays.clear();
	            listOfOverlays.add(mapOverlay); 
	            
	          /*  Button btn2 = (Button)findViewById(R.id.ok);
	            btn2.setOnClickListener(new View.OnClickListener() {
	                @Override
	                public void onClick(View v) {
	                	setContentView(R.layout.mapview);
	                }
	            });*/
	        }
	        return false;
	    }
	}
}
